package com.woniu.service.impl;

import com.woniu.dao.WorkDao;
import com.woniu.pojo.PageBean;
import com.woniu.pojo.User;
import com.woniu.pojo.Work;
import com.woniu.pojo.WorkExample;
import com.woniu.service.WorkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {
    @Resource
    private WorkDao workDao;

    @Override
    public void add(Work work) {

    }
    /**
     * 加班汇总
     *  分页查询
     *  按照申请人模糊查询
     *  按照地区查询
     *  按照时间区间查询
     *
     * @param pageBean
     * @param work
     * @return
     */
    @Override
    public List<Work> getWorkList(PageBean pageBean, Work work) {
        WorkExample workExample=new WorkExample();
        WorkExample.Criteria criteria=workExample.createCriteria();
        if(work.getUid()!=null&&!work.getUid().equals("")){
            criteria.andUidEqualTo(work.getUid());
        }
        if(work.getAddress()!=null&&!work.getAddress().equals("")){
            criteria.andAddressEqualTo(work.getAddress());
        }
        if (work.getBtime()!=null&&!("").equals(work.getBtime())){
            criteria.andBtimeGreaterThanOrEqualTo(work.getBtime());
            criteria.andBtimeLessThanOrEqualTo(work.getBtime());
        }

        Long countRow = workDao.countByExample(workExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);

        workExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        workExample.setLimit(pageBean.getPageSize());

        List<Work> workList=workDao.selectByExample(workExample);
        return workList;
    }

    /**
     * 加班审核
     *  按照审核和未审核查询
     * @param pageBean
     * @param work
     * @return
     */
    @Override
    public List<Work> getWorkAudit(PageBean pageBean, Work work) {
        WorkExample workExample=new WorkExample();
        WorkExample.Criteria criteria=workExample.createCriteria();
        if(work.getWstate()!=null&&!work.getWstate().equals("")){
            criteria.andWstateEqualTo(work.getWstate());
        }
//        else if(work.getWstate()==null){
//            criteria.andWstateEqualTo("未审核");
//        }
        Long countRow = workDao.countByExample(workExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);

        workExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        workExample.setLimit(pageBean.getPageSize());

        List<Work> workList=workDao.selectByExample(workExample);
        return workList;
    }

    /**
     *  将未审核的加班信息修改为已审核
     *
     * @param wno
     */
    @Override
    public void updateWstate(Integer wno) {
        Work work=new Work();
        work.setWno(wno);
        work.setWstate("已审核");
        workDao.updateByPrimaryKeySelective(work);
    }

    /**
     * 查询出所有work
     * @return
     */
    @Override
    public List<Work> findAllWork() {
        return workDao.selectByExample(null);
    }

    @Override
    public List<Work> getWorkByUid(Integer uid) {
        WorkExample workExample=new WorkExample();
        WorkExample.Criteria criteria = workExample.createCriteria();
        criteria.andUidEqualTo(uid);
        return workDao.selectByExample(workExample);
    }

    @Override
    public void deleteWork(Integer wno) {
        workDao.deleteByPrimaryKey(wno);
    }

    @Override
    public Work findOneWorkByWno(Integer wno) {
        return workDao.selectByPrimaryKey(wno);
    }
/**
得到当前登录用户的加班申请表
 */
    @Override
    public List<Work> getUserWorkList(PageBean pageBean, Work work, User user) {
        WorkExample workExample=new WorkExample();
        WorkExample.Criteria criteria=workExample.createCriteria();
        if(user.getUid()!=null&&!user.getUid().equals("")){
            criteria.andUidEqualTo(user.getUid());
        }
        if(work.getUid()!=null&&!work.getUid().equals("")){
            criteria.andUidEqualTo(work.getUid());
        }
        if(work.getAddress()!=null&&!work.getAddress().equals("")){
            criteria.andAddressEqualTo(work.getAddress());
        }
        if(work.getWstate()!=null&&!work.getWstate().equals("")){
            criteria.andWstateEqualTo(work.getWstate());
        }
        if (work.getBtime()!=null&&!("").equals(work.getBtime())){
            criteria.andBtimeGreaterThanOrEqualTo(work.getBtime());
            criteria.andBtimeLessThanOrEqualTo(work.getBtime());
        }
        if (work.getBnowtime()!=null&&!("").equals(work.getBnowtime())){
            criteria.andNowtimeGreaterThanOrEqualTo(work.getBnowtime());
        }
        if (work.getEnowtime()!=null&&!("").equals(work.getEnowtime())){
            criteria.andNowtimeLessThanOrEqualTo(work.getEnowtime());
        }
        Long countRow = workDao.countByExample(workExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);

        workExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        workExample.setLimit(pageBean.getPageSize());
        workExample.setOrderByClause("nowtime desc");
        List<Work> workList=workDao.selectByExample(workExample);
        return workList;
    }

    @Override
    public void updateWorkByWno(Work work) {
        workDao.updateByPrimaryKeySelective(work);
    }

    /**
     *  加班申请
     *     add
     * @param work
     */
    @Override
    public void save(Work work) {

        workDao.insertSelective(work);
    }
}
